package com.example.teaching_server.service;

import com.example.teaching_server.dto.CheckInActivityDTO;
import com.example.teaching_server.entity.CheckInActivity;
import com.example.teaching_server.repository.CheckInActivityRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 打卡活动服务
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class CheckInActivityService {

    private final CheckInActivityRepository activityRepository;

    /**
     * 获取所有打卡活动数据
     */
    public List<CheckInActivityDTO> getAllActivities() {
        log.info("查询所有打卡活动数据");

        List<CheckInActivity> activities = activityRepository.findAll();

        return activities.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    /**
     * 实体类转DTO
     */
    private CheckInActivityDTO convertToDTO(CheckInActivity activity) {
        return CheckInActivityDTO.builder()
                .id(activity.getId())
                .title(activity.getTitle())
                .startDate(activity.getStartDate())
                .endDate(activity.getEndDate())
                .checked(activity.getIsChecked())
                .totalDays(activity.getTotalDays())
                .checkedDays(activity.getCheckedDays())
                .build();
    }
}
